﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dubs_Finder.Model
{
    public class DubsWrapper : NotifyBase
    {
        private ObservableCollection<FileWrapper> buffer = new AsyncObservableCollection<FileWrapper>();
        public ObservableCollection<FileWrapper> Buffer { get { return buffer; } }
        internal String Hash;
        internal long FileSize = 0;
        private bool setDelete = false;
        private FileWrapper mainFile;

        public bool IsChecked { get { return setDelete; } set { setDelete = value; NotifyPropertyChanged("IsChecked"); Debug.WriteLine("IsChecked: {0}", setDelete); } }
        public string FileName { get { return this.mainFile.FileName; } private set { value = mainFile.FileName; } }
        public int NumDubs { get { return Buffer.Count; } }
        public int Size { get { return (int) (FileSize / 1024.0); } }
        public FileWrapper MainFile { set { mainFile = value; NotifyPropertyChanged("FileName"); } }

        internal DubsWrapper(String hash, long fileSize, FileWrapper fw)
        {
            this.mainFile = fw;
            this.Hash = hash;
            this.FileSize = fileSize;

            this.buffer.Add(fw);
            NotifyPropertyChanged("DubsWrapper");
            NotifyPropertyChanged("NumDubs");
        }

        internal bool addLookALike(FileWrapper fw)
        {
            if (String.Equals(fw.HashString, this.Hash))
            {
                this.buffer.Add(fw);
                NotifyPropertyChanged("DubsWrapper");
                NotifyPropertyChanged("NumDubs");
                return true;
            }
            return false;
        }

        internal void deleteDubs()
        {
            if (this.setDelete == true)
            {
                foreach (FileWrapper fw in this.buffer)
                {
                    if (fw != this.mainFile)
                    {
                        fw.FileRef.Delete();
                    }
                }
            }
        }


    }
}
